﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Service.IotInfo.LightingInfo;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static LJX.SmartPark.Service.AssetManage.EquipmentLedgerServeice;
using static LJX.SmartPark.Service.IotInfo.LightingInfo.LightingInfoServeice;
using DB = LJX.SmartPark.Domain.SafeManage.Record;

namespace LJX.SmartPark.Service.SafeManage.Record
{
    public class RecordServeice:IRecordServeice
    {
        private readonly IRepository<DB.Record> _recordServeice;

        public RecordServeice(IRepository<DB.Record> recordServeice)
        {
            _recordServeice = recordServeice;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> DeleteRecordAsync(DB.Record input)
        {
            var result = await _recordServeice.DeleteByIdAsync(input.MapTo<DB.Record>());
            return Response<bool>.Ok(result > 0);
        }

        /// <summary>
        /// 获取总条数
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            return _recordServeice.GetCount();
        }

        /// <summary>
        /// 显示分页
        /// </summary>
        /// <param name="fRecord"></param>
        /// <returns></returns>
        public async Task<Response<List<DB.Record>>> GetRecordAsync(FyRecord fyRecord)
        {
            var lightinginfo = await _recordServeice.GetAllListAsync();
            var result = lightinginfo.MapToList<DB.Record>();

            if (!string.IsNullOrEmpty(fyRecord.recordTypeId))
            {
                result = result.Where(a => a.RecordTypeId == int.Parse(fyRecord.recordTypeId)).ToList();
            }

            var totalCount = result.Count();
            result = result.Skip(((fyRecord.pageIndex - 1) * fyRecord.pageSize)).Take(fyRecord.pageSize).ToList();
            return Response<List<DB.Record>>.Ok(result, totalCount);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Response<DB.Record> GetRecordById(DB.Record input)
        {
            var lightinginfo = _recordServeice.GetListQuery().FirstOrDefault(m => m.Id == input.Id);
            var result = lightinginfo.MapTo<DB.Record>();
            return Response<DB.Record>.Ok(result);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PostAddRecordAsync(DB.Record input)
        {
            var result = _recordServeice.InsertAsyncById(input.MapTo<DB.Record>());

            return Response<bool>.Ok(result.Result > 0);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PutUpdRecordAsync(DB.Record input)
        {
            var result = _recordServeice.UpdateAsync(input.MapTo<DB.Record>());

            return Response<bool>.Ok(result.Result > 0);
        }

        /// <summary>
        /// 查询分页
        /// </summary>
        public class FyRecord
        {
            public int pageIndex { get; set; }
            public int pageSize { get; set; }

            public string recordTypeId { get; set; }
        }
    }
}
